unit uExceptionTestCase;

interface

uses TestFramework;

type

  TExceptionTestCase = class(TTestCase)
  private
    FExpectedException: ExceptionClass;
  public
    procedure SetExpectedException(Value :ExceptionClass);
    procedure RunTest(ATestResult :TTestResult); override;
  end;

implementation

uses SysUtils;

{ TExceptionTestCase }

procedure TExceptionTestCase.RunTest(ATestResult: TTestResult);
begin
  try
    inherited runTest(ATestResult);
    if FExpectedException <> nil then
      Fail('Expected exception ' + FExpectedException.ClassName + ' did not occur.');
  except
     on E: Exception do
     begin
       if FExpectedException = nil then
         raise
       else
         if E.ClassType <> FExpectedException then
           raise;
     end;
  end;
  { clear the exception until the next test registers an Exception }
  FExpectedException := nil;
end;

procedure TExceptionTestCase.SetExpectedException(Value: ExceptionClass);
begin
  FExpectedException := Value;
end;

end.
